<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
	
	var  fibonacci = function(n){
		
		return n<2?n:fibonacci(n-1)+fibonacci(n-2);
	}
	
	for(var i=0;i<10;i++){   //fibonacci()함수를 직접 11번 재귀로  442번 호출
		pt("//"+i+":"+fibonacci(i));
	}
	
	var fibonacci2 = function(){   
		var memo = [0,1];
		var fib = function(n){
			
			var result = memo[n];
			if(typeof result !=="number"){
				result = fib(n-1)+fib(n-2);
				memo[n]=result;
			}
			return result;
		};
		return fib;
	}();
	
	for(var i=0;i<10;i++){   //11번 직접호출 18메모이제이션 결과를 얻기위한 호출
		pt("//"+i+":"+fibonacci2(i));
	}
	
	var memoizer = function(memo,fundamental){
		var shell = function(n){
			var result = memo[n];
			if(typeof result !== "number"){
				result = fundamental(shell,n);
				memo[n]=result;
			}
			return result;
		};
		return shell;
	}
	
	var fibonacci3 = memoizer([0,1],
		function(shell,n){
		return shell(n-1)+shell(n-2);
	}		
	);
	
	for(var i=0;i<10;i++){   //11번 직접호출 18메모이제이션 결과를 얻기위한 호출
		pt("//"+i+":"+fibonacci3(i));
	}
	
	
	function pt(str){
		document.write("<br>"+str);
	}
</script>
</head>
<body>

</body>
</html>